package com.fenghongzhang.day016;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

public class MyAdapter extends BaseAdapter {

    private List<Music> list;
    private Context context;

    public MyAdapter(List<Music> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VH vh;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_layout, null);
            vh = new VH();
            vh.img = convertView.findViewById(R.id.lv_img);
            vh.title = convertView.findViewById(R.id.lv_title);
            vh.songs = convertView.findViewById(R.id.lv_songs);
            vh.time = convertView.findViewById(R.id.lv_time);
            convertView.setTag(vh);
        }else{
            vh = (VH) convertView.getTag();
        }

//        vh.img
        Music music = list.get(position);
        vh.title.setText(music.getTitle());
        Glide.with(context).load(music.getAlbumId()).into(vh.img);
        vh.time.setText(MusicUtil.fmtTime(music.getDuartion()));
        vh.songs.setText(music.getArtist());
        return convertView;


    }
    //布局
    class VH{
        ImageView img;
        TextView title;
        TextView songs;
        TextView time;
    }

}
